﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthTemplate.Infrastructure
{
    public class Tools
    {
        /// <summary>
        /// 转化话成string
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string AnyToString(object o)
        {
            try
            {
                if (o == null)
                {
                    return "";
                }
                return o.ToString().Trim();
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 转化为Int32型
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static int AnyToInt32(object o)
        {
            try
            {
                if (o == null)
                {
                    return 0;
                }

                Int32 x = 0;
                if (Int32.TryParse(o.ToString(), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture.NumberFormat, out x))
                {
                    return x;
                }
                else
                {
                    return Convert.ToInt32(AnyToDecimal(o));
                }
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 转化为Int16型
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static short AnyToShort(object o)
        {
            try
            {
                if (o == null)
                {
                    return 0;
                }

                short x = 0;
                if (short.TryParse(o.ToString(), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture.NumberFormat, out x))
                {
                    return x;
                }
                else
                {
                    return Convert.ToInt16(AnyToDecimal(o));
                }
            }
            catch
            {
                return 0;
            }
        }

        public static decimal AnyToDecimal(object o)
        {
            try
            {
                if (o == null)
                {
                    return 0;
                }

                decimal x = 0;
                if (decimal.TryParse(o.ToString(), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture.NumberFormat, out x))
                {
                    return x;
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }
        public static double AnyToDouble(object o)
        {
            try
            {
                if (o == null)
                {
                    return 0;
                }

                double x = 0;
                if (double.TryParse(o.ToString(), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture.NumberFormat, out x))
                {
                    return x;
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }
    }
}
